﻿using FoftmAging.Core;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FoftmAging
{
    public partial class HistoryChartForm : Form
    {
        private List<CheckBox> _checkBoxes = new List<CheckBox>();

        private GroupSetting _setting;
        public GroupSetting Setting { get => _setting; set => _setting = value; }

        public HistoryChartForm()
        {
            InitializeComponent();
        }

        private void HistoryChartForm_Load(object sender, EventArgs e)
        {
            var now = DateTime.Now;
            dateTimePicker1.Value = now.AddMinutes(-10);
            dateTimePicker2.Value = now;


            //初始化行
            tableLayoutPanel1.RowCount = (int)Math.Ceiling(_setting.ChannelCount * 1.0 / 3);
            tableLayoutPanel1.RowStyles.Clear();

            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
            {
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 20));
            }


            for (int i = 0; i < _setting.ChannelCount; i++)
            {
                var chb = new CheckBox
                {
                    Text = $"CH{i + 1}",
                    Checked = true
                };
                _checkBoxes.Add(chb);
                tableLayoutPanel1.Controls.Add(chb);
            }


            formsPlot1.Plot.ShowLegend(Edge.Bottom);

            this.Text += $"   -- {_setting.GroupNo}组";
        }

        private List<int> GetSelectedChannelNos()
        {
            var list = new List<int>();

            for (int i = 0; i < _checkBoxes.Count; i++)
            {
                if (_checkBoxes[i].Checked) list.Add(i + 1);
            }
            return list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var beginDt = dateTimePicker1.Value.Date;
            var endDt = dateTimePicker2.Value.Date;
            var list = new List<TimeTemperature>();
            for (int i = 0; i <= (endDt.Subtract(beginDt).TotalDays); i++)
            {
                var date = beginDt.AddDays(i);
                var filename = $"{date:yyyy-MM-dd}.csv";
                filename = Path.Combine("datas", _setting.GroupNo.ToString(), filename);
                if (File.Exists(filename))
                {
                    var lines = File.ReadAllLines(filename);
                    foreach (var line in lines)
                    {
                        var arr = line.Split(',');
                        list.Add(new TimeTemperature
                        {
                            ChannelNo = int.Parse(arr[0]),
                            Temperature = double.Parse(arr[1]),
                            Time = DateTime.Parse(arr[2])
                        });

                    }
                }
            }
            var channelNos = GetSelectedChannelNos();
            list = list.Where(w => w.Time >= dateTimePicker1.Value && w.Time <= dateTimePicker2.Value && channelNos.Contains(w.ChannelNo)).OrderBy(b => b.Time).ToList();
            formsPlot1.Plot.Clear();
            //foreach (var item in formsPlot1.Plot.Legend.GetItems())
            //{
            //        item.IsVisible = false;
            //}
            if (!list.Any()) return;

            var grps = list.GroupBy(g => g.ChannelNo);
            foreach (var grp in grps)
            {
                var list1 = grp.OrderBy(b => b.Time).ToList();
                var xs = list1.Select(s => s.Time).ToArray();
                var ys = list1.Select(s => s.Temperature).ToArray();
                var sig = formsPlot1.Plot.Add.SignalXY(xs, ys);
                sig.LegendText = $"CH{grp.Key + 1}";
            }


            formsPlot1.Plot.Axes.DateTimeTicksBottom();

            formsPlot1.Plot.Axes.SetLimits(list.First().Time.ToOADate(), list.Last().Time.AddHours(1).ToOADate(), -50, 50);
            formsPlot1.Plot.Axes.AutoScale();
            formsPlot1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (var item in _checkBoxes)
            {
                item.Checked = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (var item in _checkBoxes)
            {
                item.Checked = !item.Checked;
            }
        }
    }
}
